import 'package:dio/dio.dart';
import '../../model/TokenEntity.dart';
import '../ApiUtils.dart';
import '../R.dart';

Future<R> getToken() async {
  String url = "/api/v1.agency/transfer";
  Map<String, dynamic> map = {};
  map['_url_method'] = "POST";
  map['_url_path'] = "/connect/token";
  map['_url_querycontenttype'] = 0;
  map['_url_isapp'] = 1;
  map['_url_query'] = {"grant_type": "client_credentials"};

  dynamic methodAwait =
      await ApiUtils.methodAwait(ApiUtils.postMethod, url, param: map);

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> convert(String filePath, String type, TokenEntity tokenEntity) async {
  String url = "/v4.0/words/convert?format=$type";
  FormData formData = FormData.fromMap({
    'Document': await MultipartFile.fromFile(filePath),
  });

  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.putMethod, url,
      headers: {
        "Authorization": "${tokenEntity.token_type} ${tokenEntity.access_token}"
      },
      isUseBaseUrl: false,
      options: Options(responseType: ResponseType.bytes),
      param: formData);

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> Upload(String filePath, TokenEntity tokenEntity) async {
  //tokenEntity.access_token = Constant.token;
  List<String> pathComponents = filePath.split('/');
  String fileName = pathComponents.last;
  String url = "/v4.0/words/storage/file/$fileName";
  FormData formData = FormData.fromMap({
    'FileContent': await MultipartFile.fromFile(filePath),
  });

  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.putMethod, url,
      headers: {
        "Authorization": "${tokenEntity.token_type} ${tokenEntity.access_token}"
      },
      isUseBaseUrl: false,
      param: formData,
      isShowLog: true);

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> Split(String filePath, String format, bool ZipOutput,
    TokenEntity tokenEntity) async {
  // tokenEntity.access_token = Constant.token;
  List<String> pathComponents = filePath.split('/');
  String fileName = pathComponents.last;
  String url =
      "/v4.0/words/$fileName/split?format=$format&ZipOutput=$ZipOutput";
  // FormData formData = FormData.fromMap({
  //   'FileContent': await MultipartFile.fromFile(filePath),
  // });
  print(url);
  dynamic methodAwait = await ApiUtils.methodAwait(
    ApiUtils.putMethod,
    url,
    headers: {
      "Authorization": "${tokenEntity.token_type} ${tokenEntity.access_token}"
    },
    isUseBaseUrl: false,
    param: null,
  );

  R r = R.fromData(methodAwait);

  return r;
}

Future<R> Download(String file, TokenEntity tokenEntity) async {
  // file = "360加固助手使用手册.jpeg.zip";
  // tokenEntity.access_token = Constant.token;
  String url = "/v4.0/words/storage/file/$file";
  dynamic methodAwait = await ApiUtils.methodAwait(ApiUtils.getMethod, url,
      headers: {
        "Authorization": "${tokenEntity.token_type} ${tokenEntity.access_token}"
      },
      options: Options(responseType: ResponseType.bytes),
      isUseBaseUrl: false);
  R r = R.fromData(methodAwait);
  return r;
}
